<template>
  <div class="navigation-container">
    <!-- 侧边栏导航 -->
    <div class="sidebar" :class="{ 
      'sidebar-collapsed': isCollapsed,
      'sidebar-mobile': isMobileMenuOpen 
    }">
      <!-- 导航菜单 -->
      <nav class="sidebar-nav">
        <div class="nav-section">
          <div class="nav-section-title" v-show="!isCollapsed">主要功能</div>
          <ul class="nav-menu">
            <li 
              v-for="item in mainMenuItems" 
              :key="item.path"
              class="nav-item"
              :class="{ 'active': currentRoute === item.path }"
              @click="navigateTo(item.path)"
            >
              <div class="nav-icon">{{ item.icon }}</div>
              <div class="nav-text" v-show="!isCollapsed">{{ item.title }}</div>
              <div class="nav-tooltip" v-show="isCollapsed">{{ item.title }}</div>
            </li>
          </ul>
        </div>

        <div class="nav-section">
          <div class="nav-section-title" v-show="!isCollapsed">系统管理</div>
          <ul class="nav-menu">
            <li 
              v-for="item in systemMenuItems" 
              :key="item.path"
              class="nav-item"
              :class="{ 'active': currentRoute === item.path }"
              @click="navigateTo(item.path)"
            >
              <div class="nav-icon">{{ item.icon }}</div>
              <div class="nav-text" v-show="!isCollapsed">{{ item.title }}</div>
              <div class="nav-tooltip" v-show="isCollapsed">{{ item.title }}</div>
            </li>
          </ul>
        </div>
      </nav>

      <!-- 用户信息 -->
      <div class="sidebar-footer" v-show="!isCollapsed">
        <div class="user-info">
          <div class="user-avatar">👤</div>
          <div class="user-details">
            <div class="user-name">收银员: 张三</div>
            <div class="user-role">班次: 早班</div>
          </div>
        </div>
        <button class="logout-btn" @click="logout">
          <span class="logout-icon">🚪</span>
          <span class="logout-text">退出登录</span>
        </button>
      </div>

      <!-- 折叠按钮区域 -->
      <div class="sidebar-header">
        <button class="collapse-btn" @click="toggleCollapse">
          <span v-if="!isCollapsed">◀</span>
          <span v-else>▶</span>
        </button>
      </div>
    </div>

    <!-- 移动端遮罩层 -->
    <div 
      class="mobile-overlay" 
      v-show="isMobileMenuOpen"
      @click="closeMobileMenu"
    ></div>

    <!-- 主内容区域 -->
    <div class="main-content" :class="{ 'main-expanded': isCollapsed }">
      <!-- 顶部工具栏 -->
      <div class="top-toolbar">
        <div class="toolbar-left">
          <div class="current-page">
            <h2>{{ currentPageTitle }}</h2>
          </div>
        </div>
        <div class="toolbar-right">
          <div class="quick-actions">
            <GlobalSearch />
            <NotificationPanel />
            <HelpPanel />
          </div>
        </div>
      </div>

      <!-- 面包屑导航 -->
      <Breadcrumb />

      <!-- 快速操作工具栏 -->
      <QuickActions />

      <!-- 页面内容 -->
      <div class="page-content">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import Breadcrumb from './Breadcrumb.vue'
import QuickActions from './QuickActions.vue'
import GlobalSearch from './GlobalSearch.vue'
import NotificationPanel from './NotificationPanel.vue'
import HelpPanel from './HelpPanel.vue'

const router = useRouter()
const route = useRoute()

// 响应式数据
const isCollapsed = ref(false)
const isMobileMenuOpen = ref(false)

// 菜单项配置
const mainMenuItems = [
  {
    path: '/home',
    title: '首页',
    icon: '🏠'
  },
  {
    path: '/pos',
    title: '快速收银',
    icon: '💰'
  },
  {
    path: '/inventory',
    title: '商品管理',
    icon: '📦'
  },
  {
    path: '/members',
    title: '会员管理',
    icon: '👥'
  },
  {
    path: '/reports',
    title: '报表统计',
    icon: '📊'
  }
]

const systemMenuItems = [
  {
    path: '/settings',
    title: '系统设置',
    icon: '⚙️'
  },
  {
    path: '/backup',
    title: '数据备份',
    icon: '💾'
  }
]

// 计算属性
const currentRoute = computed(() => route.path)
const currentPageTitle = computed(() => {
  const allItems = [...mainMenuItems, ...systemMenuItems]
  const currentItem = allItems.find(item => item.path === route.path)
  return currentItem ? currentItem.title : '首页'
})

// 方法
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

const toggleMobileMenu = () => {
  isMobileMenuOpen.value = !isMobileMenuOpen.value
}

const closeMobileMenu = () => {
  isMobileMenuOpen.value = false
}

const navigateTo = (path) => {
  router.push(path)
  // 在移动端点击导航后关闭菜单
  if (window.innerWidth <= 768) {
    closeMobileMenu()
  }
}

const logout = () => {
  // 这里可以添加退出登录的逻辑
  router.push('/login')
}

// 生命周期
onMounted(() => {
  // 监听窗口大小变化
  const handleResize = () => {
    if (window.innerWidth > 768) {
      closeMobileMenu()
    }
  }
  
  window.addEventListener('resize', handleResize)
  
  // 清理函数
  onUnmounted(() => {
    window.removeEventListener('resize', handleResize)
  })
})
</script>

<style scoped>
.navigation-container {
  display: flex;
  height: 100vh;
  background: #fafafa;
  position: relative;
}

/* 移动端汉堡菜单按钮 */
.mobile-menu-btn {
  display: none;
  position: fixed;
  top: 20px;
  left: 20px;
  z-index: 1001;
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.hamburger {
  display: flex;
  flex-direction: column;
  width: 18px;
  height: 14px;
  justify-content: space-between;
}

.hamburger span {
  display: block;
  height: 2px;
  width: 100%;
  background: #333;
  transition: all 0.3s ease;
}

.hamburger.active span:nth-child(1) {
  transform: rotate(45deg) translate(4px, 4px);
}

.hamburger.active span:nth-child(2) {
  opacity: 0;
}

.hamburger.active span:nth-child(3) {
  transform: rotate(-45deg) translate(6px, -5px);
}

/* 侧边栏样式 */
.sidebar {
  width: 260px;
  background: white;
  color: #333;
  display: flex;
  flex-direction: column;
  transition: width 0.3s ease;
  border-right: 1px solid #e0e0e0;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
}

.sidebar-collapsed {
  width: 70px;
}

.sidebar-header {
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #e0e0e0;
  background: #f8f9fa;
}

.collapse-btn {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  font-size: 14px;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.collapse-btn:hover {
  background: #e9ecef;
}

/* 导航菜单 */
.sidebar-nav {
  flex: 1;
  padding: 20px 0;
  overflow-y: auto;
}

.nav-section {
  margin-bottom: 24px;
}

.nav-section-title {
  padding: 0 20px 8px;
  font-size: 11px;
  font-weight: 600;
  color: #999;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.nav-menu {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  padding: 12px 20px;
  cursor: pointer;
  transition: background-color 0.2s ease;
  margin: 2px 0;
}

.nav-item:hover {
  background: #f8f9fa;
}

.nav-item.active {
  background: #e3f2fd;
  color: #1976d2;
  border-right: 3px solid #1976d2;
}

.nav-icon {
  font-size: 18px;
  margin-right: 12px;
  min-width: 18px;
  text-align: center;
}

.nav-text {
  font-size: 14px;
  font-weight: 500;
}

.nav-tooltip {
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  background: #333;
  color: white;
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 12px;
  white-space: nowrap;
  z-index: 1000;
  margin-left: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.nav-item:hover .nav-tooltip {
  opacity: 1;
}

/* 侧边栏底部 */
.sidebar-footer {
  padding: 16px 20px;
  border-bottom: 1px solid #e0e0e0;
  background: #f8f9fa;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  padding: 12px;
  background: white;
  border-radius: 6px;
  border: 1px solid #e0e0e0;
}

.user-avatar {
  font-size: 20px;
  margin-right: 10px;
}

.user-name {
  font-size: 13px;
  font-weight: 600;
  color: #333;
  margin-bottom: 2px;
}

.user-role {
  font-size: 11px;
  color: #666;
}

.logout-btn {
  width: 100%;
  background: #dc3545;
  border: none;
  color: white;
  padding: 10px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s ease;
  font-size: 13px;
  font-weight: 500;
}

.logout-btn:hover {
  background: #c82333;
}

.logout-icon {
  margin-right: 6px;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  transition: margin-left 0.3s ease;
  background: #fafafa;
}

.main-expanded {
  margin-left: 0;
}

/* 顶部工具栏 */
.top-toolbar {
  background: white;
  padding: 8px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e0e0e0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.current-page h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.quick-actions {
  display: flex;
  gap: 4px;
}

.action-btn {
  position: relative;
  background: none;
  border: 1px solid #e0e0e0;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.action-btn:hover {
  background: #f8f9fa;
  border-color: #1976d2;
}

.action-icon {
  font-size: 16px;
  color: #666;
}

.notification-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: #dc3545;
  color: white;
  font-size: 10px;
  padding: 2px 4px;
  border-radius: 8px;
  min-width: 14px;
  text-align: center;
}

/* 页面内容 */
.page-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background: white;
  margin: 8px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  border: 1px solid #e0e0e0;
}

/* 移动端遮罩层 */
.mobile-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  z-index: 999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .mobile-menu-btn {
    display: block;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .sidebar-mobile {
    transform: translateX(0);
  }

  .main-content {
    margin-left: 0;
  }

  .top-toolbar {
    padding: 6px 16px;
    margin-top: 50px;
  }

  .page-content {
    padding: 12px;
    margin: 6px;
  }

  .sidebar-header {
    justify-content: center;
    padding: 8px;
  }
  
  .collapse-btn {
    font-size: 12px;
    padding: 6px;
  }
  
  .current-page h2 {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .toolbar-right {
    flex-direction: column;
    gap: 6px;
  }

  .top-toolbar {
    flex-direction: column;
    gap: 6px;
    text-align: center;
    padding: 6px 10px;
  }

  .current-page h2 {
    font-size: 12px;
  }
}
</style> 